﻿using MySchool;
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;

namespace MyKTV.wxt
{
    public partial class Form2 : Form
    {
        DBHelper db = new DBHelper();
        DataSet ds = new DataSet();
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            string sql = "select resource_path from resource_path where resource_type = 'song'";
            try
            {
                db.OpenConnection();
                SqlCommand sqlCommand = new SqlCommand(sql, db.Connection);
                textBoxEx1.Text = sqlCommand.ExecuteScalar().ToString();
                this.textBoxEx1.Enabled = false;
            }
            catch (Exception i)
            {
                MessageBox.Show(i.Message);
            }
            finally
            {
                db.CloseConnection();
            }
        }
        private void button1_Click(object sender, EventArgs e){
            FolderBrowserDialog folder = new FolderBrowserDialog();
            DialogResult result = folder.ShowDialog();
            if (result != DialogResult.OK)
            {
                MessageBox.Show("请选择新路径！", "提示");
            }
            else
            {
                this.textBoxEx2.Text = folder.SelectedPath;
                // 查询数据库中存储的歌手图片资源路径
                // 初始化查询过了
                db.OpenConnection();
                // 将原路径下的文件复制到新路径下                            
                string[] files = Directory.GetFiles(textBoxEx1.Text);
                string fileName = ""; // 文件名（不含路径）
                string dest = "";   // 目标文件名（含路径）
                foreach (string source in files)
                {
                    fileName = Path.GetFileName(source);
                    dest = this.textBoxEx2.Text.Trim() + "\\" + fileName;
                    File.Copy(source, dest);
                }
                string sql = "update resource_path set resource_path = N'" + textBoxEx2.Text + "' where resource_type = 'song'";
                SqlCommand command = new SqlCommand(sql,db.Connection);
                int count = command.ExecuteNonQuery();
                if (count > 0)
                {
                    MessageBox.Show("修改成功！");
                    this.textBoxEx1.Text = this.textBoxEx2.Text;
                    this.textBoxEx2.Text = "";
                }
                else
                {
                    MessageBox.Show("修改失败！");
                }
            }
        }
    }
}
